﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using HalconDotNet;

namespace wtf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
       // HObject ho_Image, ho_GrayImage, ho_Regions;
        HTuple hv_WindowHandle = null;


        HObject ho_Image2, ho_GrayImage, ho_ImageInvert;
        HObject ho_Regions, ho_Region, ho_RegionFillUp, ho_RegionDilation, ho_ConnectedRegions;
        HObject ho_SelectedRegions, ho_RegionFillUp1, ho_RegionErosion;
        HObject ho_ConnectedRegions1, ho_SelectedRegions1, ho_ImageReduced;


        HObject ho_Region1, ho_ConnectedRegions2, ho_SelectedRegions2;
        HObject ho_SortedRegions;
        

        // Local control variables 
        HTuple hv_OCRHandle = null, hv_Class = null;
        HTuple hv_Confidence = null, hv_i = null;
        // Initialize local and output iconic variables 
        string ImagePath;
        private void button1_Click(object sender, EventArgs e)
        {
            

            openFileDialog1.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*";
            openFileDialog1.RestoreDirectory = true;

            HOperatorSet.GenEmptyObj(out ho_Image2);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageInvert);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
            HOperatorSet.GenEmptyObj(out ho_RegionDilation);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionFillUp1);
            HOperatorSet.GenEmptyObj(out ho_RegionErosion);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions1);
            HOperatorSet.GenEmptyObj(out ho_ImageReduced);
            HOperatorSet.GenEmptyObj(out ho_Region1);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SelectedRegions2);
            HOperatorSet.GenEmptyObj(out ho_SortedRegions);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            //HOperatorSet.GenEmptyObj(out ho_Regions);
            ho_Image2.Dispose();



            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ImagePath = openFileDialog1.FileName;
                HOperatorSet.ReadImage(out ho_Image2, ImagePath);
            }

        }
        //private void button3_Click(object sender, EventArgs e)
        //{
        //    //显示OpenWindow
            
        //    HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
        //    HDevWindowStack.Push(hv_WindowHandle);

        //    ho_GrayImage.Dispose();
        //    HOperatorSet.Rgb1ToGray(ho_Image2, out ho_GrayImage);
        //    ho_ImageInvert.Dispose();
        //    HOperatorSet.InvertImage(ho_GrayImage, out ho_ImageInvert);
        //    ho_Region.Dispose();
        //    HOperatorSet.Threshold(ho_ImageInvert, out ho_Region, 128, 255);
        //    ho_RegionFillUp.Dispose();
        //    HOperatorSet.FillUp(ho_Region, out ho_RegionFillUp);
        //    ho_RegionDilation.Dispose();
        //    HOperatorSet.DilationRectangle1(ho_RegionFillUp, out ho_RegionDilation, 11,
        //        11);
        //    ho_ConnectedRegions.Dispose();
        //    HOperatorSet.Connection(ho_RegionDilation, out ho_ConnectedRegions);
        //    ho_SelectedRegions.Dispose();
        //    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("rectangularity")).TupleConcat(
        //        "area"), "and", (new HTuple(0.73853)).TupleConcat(19954.1), (new HTuple(1)).TupleConcat(
        //        50000));
        //    //填充*
        //    ho_RegionFillUp1.Dispose();
        //    HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp1);
        //    //腐蚀*
        //    ho_RegionErosion.Dispose();
        //    HOperatorSet.ErosionCircle(ho_RegionFillUp1, out ho_RegionErosion, 6);
        //    ho_ConnectedRegions1.Dispose();
        //    HOperatorSet.Connection(ho_RegionErosion, out ho_ConnectedRegions1);
        //    ho_SelectedRegions1.Dispose();
        //    HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1,
        //        "max_area", 70);
        //    ho_ImageReduced.Dispose();
        //    HOperatorSet.ReduceDomain(ho_ImageInvert, ho_SelectedRegions1, out ho_ImageReduced
        //        );
        //    ho_Region1.Dispose();
        //    HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, 137);
        //    ho_ConnectedRegions2.Dispose();
        //    HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions2);
        //    ho_SelectedRegions2.Dispose();
        //    HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area",
        //        "and", 401.21, 600.92);
        //    ho_SortedRegions.Dispose();
        //    HOperatorSet.SortRegion(ho_SelectedRegions2, out ho_SortedRegions, "character",
        //        "true", "row");
        //    if (HDevWindowStack.IsOpen())
        //    {
        //        HOperatorSet.DispObj(ho_SortedRegions, HDevWindowStack.GetActive());
        //    }

        //    HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
        //    HOperatorSet.DoOcrMultiClassMlp(ho_SortedRegions, ho_ImageReduced, hv_OCRHandle, out hv_Class, out hv_Confidence);
        //    string str = hv_Class.ToString();
        //    char[] c = str.ToCharArray();
        //    string str1 = string.Format("{0}{1}{2}{3}{4}{5}", c[2], c[7], c[12], c[17], c[22], c[27]);
        //    textBox1.Text = str1;
        //}
        //private void button5_Click(object sender, EventArgs e)
        //{
        //    HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
        //    HDevWindowStack.Push(hv_WindowHandle);

        //    ho_GrayImage.Dispose();
        //    HOperatorSet.Rgb1ToGray(ho_Image2, out ho_GrayImage);
        //    ho_ImageInvert.Dispose();
        //    HOperatorSet.InvertImage(ho_GrayImage, out ho_ImageInvert);
        //    ho_Regions.Dispose();
        //    HOperatorSet.Threshold(ho_ImageInvert, out ho_Regions, 0, 12);
        //    ho_ConnectedRegions.Dispose();
        //    HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
        //    ho_SelectedRegions.Dispose();
        //    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("area")).TupleConcat(
        //        "height"), "and", (new HTuple(1591.74)).TupleConcat(69.91), (new HTuple(3821.1)).TupleConcat(
        //        161.28));

        //    if (HDevWindowStack.IsOpen())
        //    {
        //        HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive());
        //    }

        //    HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
        //    HOperatorSet.DoOcrMultiClassMlp(ho_SelectedRegions, ho_ImageInvert, hv_OCRHandle,out hv_Class, out hv_Confidence);

        //    string str = hv_Class.ToString();
        //    string str2= str.Replace("I","1");
        //    char[] c = str2.ToCharArray();
        //    string str3 = string.Format("{0}{1}{2}{3}{4}{5}", c[2], c[7], c[12], c[17], c[22], c[27]);
        //    textBox1.Text = str3;
        //}

        private void button6_Click(object sender, EventArgs e)
        {
            string str_sw = ImagePath.Substring(32,2);

            switch (str_sw)
            {
                case "01":
                    //textBox2.Text = "111";

                    //显示OpenWindow

                    HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
                    HDevWindowStack.Push(hv_WindowHandle);

                    ho_GrayImage.Dispose();
                    HOperatorSet.Rgb1ToGray(ho_Image2, out ho_GrayImage);
                    ho_ImageInvert.Dispose();
                    HOperatorSet.InvertImage(ho_GrayImage, out ho_ImageInvert);
                    ho_Region.Dispose();
                    HOperatorSet.Threshold(ho_ImageInvert, out ho_Region, 128, 255);
                    ho_RegionFillUp.Dispose();
                    HOperatorSet.FillUp(ho_Region, out ho_RegionFillUp);
                    ho_RegionDilation.Dispose();
                    HOperatorSet.DilationRectangle1(ho_RegionFillUp, out ho_RegionDilation, 11,
                        11);
                    ho_ConnectedRegions.Dispose();
                    HOperatorSet.Connection(ho_RegionDilation, out ho_ConnectedRegions);
                    ho_SelectedRegions.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("rectangularity")).TupleConcat(
                        "area"), "and", (new HTuple(0.73853)).TupleConcat(19954.1), (new HTuple(1)).TupleConcat(
                        50000));
                    //填充*
                    ho_RegionFillUp1.Dispose();
                    HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp1);
                    //腐蚀*
                    ho_RegionErosion.Dispose();
                    HOperatorSet.ErosionCircle(ho_RegionFillUp1, out ho_RegionErosion, 6);
                    ho_ConnectedRegions1.Dispose();
                    HOperatorSet.Connection(ho_RegionErosion, out ho_ConnectedRegions1);
                    ho_SelectedRegions1.Dispose();
                    HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1,
                        "max_area", 70);
                    ho_ImageReduced.Dispose();
                    HOperatorSet.ReduceDomain(ho_ImageInvert, ho_SelectedRegions1, out ho_ImageReduced
                        );
                    ho_Region1.Dispose();
                    HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, 137);
                    ho_ConnectedRegions2.Dispose();
                    HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions2);
                    ho_SelectedRegions2.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area",
                        "and", 401.21, 600.92);
                    ho_SortedRegions.Dispose();
                    HOperatorSet.SortRegion(ho_SelectedRegions2, out ho_SortedRegions, "character",
                        "true", "row");
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_SortedRegions, HDevWindowStack.GetActive());
                    }

                    HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
                    HOperatorSet.DoOcrMultiClassMlp(ho_SortedRegions, ho_ImageReduced, hv_OCRHandle, out hv_Class, out hv_Confidence);
                    string str1_1 = hv_Class.ToString();
                    char[] c1 = str1_1.ToCharArray();
                    string str1_2 = string.Format("{0}{1}{2}{3}{4}{5}", c1[2], c1[7], c1[12], c1[17], c1[22], c1[27]);
                    textBox1.Text = str1_2;
                    break;
                case "02":
                    //textBox2.Text = "222";
                    HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
                    HDevWindowStack.Push(hv_WindowHandle);

                    ho_GrayImage.Dispose();
                    HOperatorSet.Rgb1ToGray(ho_Image2, out ho_GrayImage);
                    ho_ImageInvert.Dispose();
                    HOperatorSet.InvertImage(ho_GrayImage, out ho_ImageInvert);
                    ho_Regions.Dispose();
                    HOperatorSet.Threshold(ho_ImageInvert, out ho_Regions, 0, 12);
                    ho_ConnectedRegions.Dispose();
                    HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                    ho_SelectedRegions.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("area")).TupleConcat(
                        "height"), "and", (new HTuple(1591.74)).TupleConcat(69.91), (new HTuple(3821.1)).TupleConcat(
                        161.28));

                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive());
                    }

                    HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
                    HOperatorSet.DoOcrMultiClassMlp(ho_SelectedRegions, ho_ImageInvert, hv_OCRHandle, out hv_Class, out hv_Confidence);

                    string str2_1 = hv_Class.ToString();
                    string str2_2 = str2_1.Replace("I", "1");
                    char[] c2 = str2_2.ToCharArray();
                    string str2_3 = string.Format("{0}{1}{2}{3}{4}{5}", c2[2], c2[7], c2[12], c2[17], c2[22], c2[27]);
                    textBox1.Text = str2_3;
                    break;
                case "03":
                    //textBox2.Text = "333";
                    HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
                    HDevWindowStack.Push(hv_WindowHandle);

                    ho_GrayImage.Dispose();
                    HOperatorSet.Rgb1ToGray(ho_Image2, out ho_GrayImage);
                    ho_ImageInvert.Dispose();
                    HOperatorSet.InvertImage(ho_GrayImage, out ho_ImageInvert);
                    ho_Regions.Dispose();
                    HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 162, 218);
                    ho_ConnectedRegions.Dispose();
                    HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
                    ho_SelectedRegions.Dispose();
                    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, ((new HTuple("area")).TupleConcat(
                        "height")).TupleConcat("width"), "and", ((new HTuple(80.38)).TupleConcat(
                        23.42)).TupleConcat(10.88), ((new HTuple(101.17)).TupleConcat(25.31)).TupleConcat(
                        13.5));
                    ho_SortedRegions.Dispose();
                    HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "character",
                        "true", "row");

                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive());
                    }

                    //识别
                    HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
                    HOperatorSet.DoOcrMultiClassMlp(ho_SortedRegions, ho_ImageInvert, hv_OCRHandle, out hv_Class, out hv_Confidence);
                    string str3_1 = hv_Class.ToString();
                    char[] c3 = str3_1.ToCharArray();
                    string str3_2 = string.Format("{0}{1}{2}{3}{4}{5}", c3[2], c3[7], c3[12], c3[17], c3[22], c3[27]);
                    textBox1.Text = str3_2;
                    break;
                default:
                    MessageBox.Show("不适用==.");
                    break;

            }
                
        }

        //private void button4_Click(object sender, EventArgs e)
        //{
        //    HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
        //    HDevWindowStack.Push(hv_WindowHandle);

        //    ho_GrayImage.Dispose();
        //    HOperatorSet.Rgb1ToGray(ho_Image2, out ho_GrayImage);
        //    ho_ImageInvert.Dispose();
        //    HOperatorSet.InvertImage(ho_GrayImage, out ho_ImageInvert);
        //    ho_Regions.Dispose();
        //    HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 162, 218);
        //    ho_ConnectedRegions.Dispose();
        //    HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions);
        //    ho_SelectedRegions.Dispose();
        //    HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, ((new HTuple("area")).TupleConcat(
        //        "height")).TupleConcat("width"), "and", ((new HTuple(80.38)).TupleConcat(
        //        23.42)).TupleConcat(10.88), ((new HTuple(101.17)).TupleConcat(25.31)).TupleConcat(
        //        13.5));
        //    ho_SortedRegions.Dispose();
        //    HOperatorSet.SortRegion(ho_SelectedRegions, out ho_SortedRegions, "character",
        //        "true", "row");

        //    if (HDevWindowStack.IsOpen())
        //    {
        //        HOperatorSet.DispObj(ho_SelectedRegions, HDevWindowStack.GetActive());
        //    }

        //    //识别
        //    HOperatorSet.ReadOcrClassMlp("Industrial_0-9A-Z_NoRej.omc", out hv_OCRHandle);
        //    HOperatorSet.DoOcrMultiClassMlp(ho_SortedRegions, ho_ImageInvert, hv_OCRHandle,out hv_Class, out hv_Confidence);
        //    string str = hv_Class.ToString();
        //    char[] c = str.ToCharArray();
        //    string str1 = string.Format("{0}{1}{2}{3}{4}{5}", c[2], c[7], c[12], c[17], c[22], c[27]);

        //}

    }
}
